﻿using System.Linq;
using ISSMoviePlex.DAL.Models;
using System.Collections.Generic;

namespace ISSMoviePlex.DAL.Repositories
{
    public class TicketRepository : BaseRepository<int, Ticket, TicketRepository>
    {
        public override Ticket FindById(int id)
        {
            return Context.Tickets.SingleOrDefault(t => t.TicketId == id);
        }

        public int GetSeatNo(int scheduleId)
        {
            //var maxSeatNo = Context.Tickets.Count(t => t.TicketId != -1);
            //var sch = Context.Schedules.SingleOrDefault(x => x.ScheduleId == scheduleId);
            //if (sch != null && (maxSeatNo + 1) <= sch.SeatAvailable)
            //{
            //    return maxSeatNo + 1;
            //}
            //return -1;
            return Context.Tickets.Count(t => t.ScheduleId == scheduleId);
        }

        public List<TicketViewModel> PopulateTicketViewGrid(int movieId)
        {
            var sl = (from a in Context.Schedules
                      join b in Context.Movies on a.MovieId equals movieId
                      select new
                      {
                          a.ScheduleId,
                          b.MovieName,
                          a.RoomId,
                          a.SeatAvailable,
                          a.ShowTime
                      });
            return sl.Select(s => new TicketViewModel
            {
                ScheduleId = s.ScheduleId,
                MovieName = s.MovieName,
                RoomId = s.RoomId,
                SeatAvailable = s.SeatAvailable,
                ShowTime = s.ShowTime
            }).ToList();
        }
    }
}
